package com.uzai.console.service.remoteapi.impl;

import com.alibaba.fastjson.JSONObject;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.constant.Const;
import com.uzai.console.para.CdkeysOldCardExchangeRemoteApiPara;
import com.uzai.console.service.remoteapi.CdkeysRemoteApiService;
import com.uzai.console.vo.remoteapi.cdkey.CdkeyConvertVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: liuqi
 * @Date: 2019/11/27 11:23
 * @Description:
 */
@Service
public class CdkeysRemoteApiServiceImpl implements CdkeysRemoteApiService {

    private static Logger logger = LoggerFactory.getLogger(CdkeysRemoteApiServiceImpl.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;

    /**
     * 远程2.0旧卡替换3.0新卡授权接口
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param cdkeysOldCardExchangeRemoteApiPara
     * @return null
     */
    public List<CdkeyConvertVo> convert(CdkeysOldCardExchangeRemoteApiPara cdkeysOldCardExchangeRemoteApiPara) {

        List<CdkeyConvertVo> cdkeyConvertVoList = new ArrayList<>();

        try{
            String remoteUrl = uzaiConsoleSysConfig.getOldCardExchangeUrl();
            Map<String, Object> param = new HashMap<>();
            param.put("key", cdkeysOldCardExchangeRemoteApiPara.getKey());
            param.put("convert_code", cdkeysOldCardExchangeRemoteApiPara.getConvert_code());
            param.put("cards", cdkeysOldCardExchangeRemoteApiPara.getCards());
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<Map<String, Object>> request = new HttpEntity<>(param, headers);

            logger.info("--oldCardExchange remoteApi---url={}, para={}", remoteUrl, JSONObject.toJSONString(param));

            ResponseEntity<String> response = restTemplate.postForEntity(remoteUrl, request, String.class);
            if (response.getStatusCode() != HttpStatus.OK){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
            }
            try {
                String body = response.getBody();
                logger.info(body);
                cdkeyConvertVoList = JSONObject.parseArray(body, CdkeyConvertVo.class);
            }catch (Exception e){
                logger.error("--oldCardExchange convert request error..{}", e.getMessage());
            }

        }catch (Exception e){
            logger.error("--oldCardExchange convert request error..{}", e.getMessage());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
        }

        return cdkeyConvertVoList;
    }

}
